package yxy.game.pm2.command.godBeast;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.GodBeastEquipPosition;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.PackageHandle;
import yxy.game.pm2.module.godBeast.PickedGodBeastModule;

/**
 * @author wrs
 * @name CMD_PickedGodBeastEquipCheck
 * @descrition 神兽查询
 * @since 2022-05-31
 */
@MessageType(proto = Command.PickedGodBeastEquipCheck,classtype = PickedGodBeastMessage.PickedGodBeastEquipCheckRequest.class)
public class CMD_PickedGodBeastEquipCheck extends Command<PickedGodBeastMessage.PickedGodBeastEquipCheckRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastEquipCheckRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int equippos = request.getEquippos(); //装备位置，爪：11，魂：12，环：13

        PickedGodBeastMessage.PickedGodBeastEquipCheckResponse.Builder response = PickedGodBeastMessage.PickedGodBeastEquipCheckResponse.newBuilder();
        //查找玩家背包中的神兽装备
        try {
            GodBeastEquipPosition equipPosition = GodBeastEquipPosition.forNumber(equippos);
            if (equipPosition == null){
                send(messageSource, response, Result.ParamError);
                return;
            }

            PickedGodBeastModule module = PickedGodBeastModule.get(playerCode);
            if (module == null){
                send(messageSource, response, Result.ParamError);
                return;
            }

            PackageMessage.Package.Builder pack = new PackageHandle().getEquipPackageByPos(playerCode, equippos);
            response.setPack(pack);
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.PickedGodBeastEquipCheckResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
